Material = Material or Class("Material")


---@diagnostic disable-next-line: duplicate-set-field
function Material:ctor(shaderName, vertexFormat)
    self.shaderName = shaderName
    self.shader = Shader:get(shaderName)
    self.uniforms = {}
    self.macros = {}
    self.vertexFormat = vertexFormat or {
        {"VertexPosition", "float", 2},
        {"VertexTexCoord", "float", 2},
        {"VertexColor", "byte", 4}
    }
end

-- 设置宏，通过宏获取对应的shader变体
function Material:setMacro(macros)
    self.shader = Shader:get(self.shaderName, macros)
end

function Material:setShader(shader)
    self.shader = shader
end

function Material:setTexture(texture)
    self.texture = texture
end

function Material:setColor(key, color)
    self.uniforms[key] = {color.r, color.g, color.b, color.a}
end

function Material:set(key, ...)
    self.uniforms[key] = {...}
    Console.log(key, ...)
    self.shader:send(key, ...)
end

function Material:clone()
    local m = Material.new(self.shaderName, self.vertexFormat)
    for k, v in ipairs(self.uniforms) do
        local t = table.deepClone(v)
        m:set(k, t)
    end
    return m
end

Material.default = Material.new("default")
Material.textDefault = Material.new("default")